package com.platform.web.controller.data;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.BaseLocation;
import com.platform.base.domain.MeterInfo;
import com.platform.base.domain.MeterType;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.IMeterInfoService;
import com.platform.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.data.domain.MeterData;
import com.platform.data.service.IMeterDataService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;

/**
 * 热采集数据Controller
 * 
 * @author wangqi
 * @date 2020-09-27
 */
@Controller
@RequestMapping("/data/meter")
public class MeterDataController extends BaseController
{
    private String prefix = "data/meter";

    @Autowired
    private IMeterDataService meterDataService;

    @Autowired
    private IMeterInfoService meterInfoService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @RequiresPermissions("data:meter:view")
    @GetMapping(value = { "", "/{meterId}" })
    public String data(@PathVariable(value = "meterId", required = false) String meterId, ModelMap mmap)
    {
        if(meterId==null){
            meterId = "";
        }
        mmap.put("meterId", meterId);
        mmap.put("location", new BaseLocation());
        return prefix + "/data";
    }

    /**
     * 根据地址查询热表信息
     * @param locationId
     * @param mmap
     * @return
     */
    @RequiresPermissions("data:meter:view")
    @GetMapping("/location/{locationId}")
    public String location(@PathVariable(value = "locationId") String locationId, ModelMap mmap)
    {
        String meterIds = "";
        List<MeterInfo> list = meterInfoService.list(new QueryWrapper<MeterInfo>().eq("location_id", locationId));
        for(MeterInfo meterInfo : list){
            if(meterIds.equals("")){
                meterIds = meterInfo.getMeterId();
            }else{
                meterIds = meterIds + "," + meterInfo.getMeterId();
            }
        }
        mmap.put("meterId", meterIds);
        mmap.put("location", baseLocationService.getById(locationId));
        return prefix + "/data";
    }

    /**
     * 查询热采集数据列表
     */
    @RequiresPermissions("data:meter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeterData meterData)
    {
        startPage();
        if(meterData.getMeterId() !=null && !meterData.getMeterId().equals("")){
            meterData.setLocationId(null);
        }
        List<MeterData> list = meterDataService.selectMeterDataList(meterData);
        return getDataTable(list);
    }

    /**
     * 导出热采集数据列表
     */
    @RequiresPermissions("data:meter:export")
    @Log(title = "热采集数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MeterData meterData)
    {
        if(meterData.getMeterId() !=null && !meterData.getMeterId().equals("")){
            meterData.setLocationId(null);
        }
        List<MeterData> list = meterDataService.selectMeterDataList(meterData);
        ExcelUtil<MeterData> util = new ExcelUtil<MeterData>(MeterData.class);
        return util.exportExcel(list, "热表采集数据");
    }

    /**
     * 读取热表数据
     */
    @Log(title = "读取热表数据", businessType = BusinessType.OTHER)
    @RequiresPermissions("data:meter:check")
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(String meterId) throws InterruptedException {
        String message = meterDataService.checkData(meterId);
        return AjaxResult.success(message);
    }

}
